/**
 * @Author       : Mo 2427995270@qq.com
 * @Date         : 2025-07-28 22:56:14
 * @LastEditTime : 2025-07-30 01:10:07
 * @FilePath     : \ZN-1000-APP\hal_driver\ll_low_power.c
 * @Description  : 低功耗低层驱动
 * @Version      : V1.0
 * @History      :
 * @Note         :
 */
/***************************************Includes***********************************/
#include "ll_low_power.h"

#if defined(GD32F407)
#include "gd32f4xx.h"
#elif defined(STM32F40_41xxx)
#include "stm32f4xx.h"
#elif defined(GD32F10X_HD)
#include "gd32f10x.h"
#elif defined(STM32F10X_HD)
#include "stm32f10x.h"
#elif defined(PY32F071xB)
#include "py32f0xx.h"
#else
#error "No device selected"
#endif

/************************************Private Macros********************************/
#if defined(PY32F071xB)
#define Delay 40 * 128
#endif

/**********************************Private Statement*******************************/

static void APP_DelayNops(uint32_t Nops);
#if defined(PY32F071xB)
__IO uint32_t RatioNops;
#endif

/***********************************Public Variables*******************************/



/**********************************Private Variables*******************************/



/**********************************Public Functions********************************/

void low_power_init(void) {
#if defined(PY32F071xB)
    RatioNops = Delay * (SystemCoreClock / 1000000U) / 4;
#endif
}

/**
 * @brief 进入停止模式 中断唤醒
 * @note 唤醒后会自动使用HSI时钟，HSE时钟需重新配置
 */
void low_power_enter_stop_interrupt(void) {
#if defined(PY32F071xB)
    APP_DelayNops(RatioNops);
    PWR_StopModeConfigTypeDef PwrStopModeCfg = {0};
    PwrStopModeCfg.LPVoltSelection = PWR_STOPMOD_LPR_VOLT_0P8V; // 0.8V
    PwrStopModeCfg.FlashDelay = PWR_WAKEUP_HSIEN_AFTER_MR;
    PwrStopModeCfg.WakeUpHsiEnableTime = PWR_WAKEUP_FLASH_DELAY_1US;
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#else
#error "todo"
#endif
}
/**
 * @brief 进入停止模式 事件唤醒
 * @note 唤醒后会自动使用HSI时钟，HSE时钟需重新配置
 */
void low_power_enter_stop_event(void) {
#if defined(PY32F071xB)
    APP_DelayNops(RatioNops);
    PWR_StopModeConfigTypeDef PwrStopModeCfg = {0};
    PwrStopModeCfg.LPVoltSelection = PWR_STOPMOD_LPR_VOLT_0P8V; // 0.8V
    PwrStopModeCfg.FlashDelay = PWR_WAKEUP_HSIEN_AFTER_MR;
    PwrStopModeCfg.WakeUpHsiEnableTime = PWR_WAKEUP_FLASH_DELAY_1US;
    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE);
#else
#error "todo"
#endif
}
/**
 * @brief 进入睡眠模式 中断唤醒
 * @note null
 */
void low_power_enter_sleep_interrupt(void) {
#if defined(PY32F071xB)
    APP_DelayNops(RatioNops);
    HAL_PWR_EnterSLEEPMode(PWR_SLEEPENTRY_WFI); // 进入睡眠模式
#else
#error "todo"
#endif
}
/**
 * @brief 进入睡眠模式 事件唤醒
 * @note null
 */
void low_power_enter_sleep_event(void) {
#if defined(PY32F071xB)
    APP_DelayNops(RatioNops);
    HAL_PWR_EnterSLEEPMode(PWR_SLEEPENTRY_WFE); // 进入睡眠模式
#else
#error "todo"
#endif
}
/**
 * @brief 使能调试停止模式
 * @note null
 */
void low_power_enable_dbg_stop(void) {
#if defined(PY32F071xB)
    HAL_DBGMCU_EnableDBGMCUStopMode();
#else
#error "todo"
#endif
}
/**
 * @brief 禁能调试停止模式
 * @note null
 */
void low_power_disable_dbg_stop(void) {
#if defined(PY32F071xB)
    HAL_DBGMCU_DisableDBGMCUStopMode();
#else
#error "todo"
#endif
}
/**
 * @brief 使能调试睡眠模式
 * @note null
 */
void low_power_enable_dbg_sleep(void) {
#if defined(PY32F071xB)
    HAL_DBGMCU_EnableDBGMCUSleepMode();
#else
#error "todo"
#endif
}
/**
 * @brief 禁能调试睡眠模式
 * @note null
 */
void low_power_disable_dbg_sleep(void) {
#if defined(PY32F071xB)
    HAL_DBGMCU_DisableDBGMCUSleepMode();
#else
#error "todo"
#endif
}

/**********************************Private Functions*******************************/

/**
 * @brief  Delayed by NOPS
 * @param  None
 * @retval None
 */
static void APP_DelayNops(uint32_t Nops) {
    for (uint32_t i = 0; i < Nops; i++) {
        __NOP();
    }
}

/* [] END OF FILE */
